require "Net/send_Msg"
require "Net/TrialHandel"
require 'View/UIBase/UIBase'

UIChaosTest = UIBase.New(PanelNames.UIChaosTest);
local skills = {};
local skillCount = 0;
local _bfxwPlayTime = 2000 -- 暴风峡湾入场动画持续时间（毫秒）
local change_type = nil
local hero_list_data = nil
local hero_type_data = nil
local activity_title = nil
local activity_desp = nil

local activity_boss_battlemap = nil
local activity_trials_bossid = nil
local activity_trials_times = nil -- 挑战次数
local activity_trials_icon = nil

function UIChaosTest.Init(func, _type)
    change_type = _type or 1 -- change_type 1为暴风峡湾 2 新英雄挑战活动
    UIBase.Init(UIChaosTest, func);
end

--定义控件
function UIChaosTest:OnCreate()
    -- self.titleCom = self:GetChild("Title");
    self.ctrl_activity_type = self:GetController("ActivityType")
    self.backBtn = self:GetChild("BackButton");
    self.tipsBtn = self:GetChild("Tips");
    self.rankBtn = self:GetChild("RankList");
    self.BossModel = self:GetChild("BossModel");
    self.finalRewardBtn = self:GetChild("FinalReward");
    self.nextPreviewBtn = self:GetChild("NextPreview");
    self.battleRecordBtn = self:GetChild("BattleRecord");
    self.bossNameText = self:GetChild("BossName");
    self.bossTypeLoader = self:GetChild("BossType");
    self.bossSkillList = self:GetChild("BossSkill");
    self.totalDmgText = self:GetChild("TotalDmg");
    self.maxRecordText = self:GetChild("MaxRecord");
    self.FriendRank = self:GetChild("FriendRank");
    self.challengeBtn = self:GetChild("ChallengeButton");
    self.friendList = self:GetChild("FriendRank"):GetChild("FriendList");
    self.timeCountText = self:GetChild("Text_TimeCount");
    self.titleSkill = self:GetChild("titleSkill");
    self.titleSkillName = self.titleSkill:GetChild("Word");
    self.titleSkillDecName = self.titleSkill:GetChild("WordName");
    self.titleSkillDecTips = self.titleSkill:GetChild("WordTips");
    self.tipsShowController = self:GetController("SkillTips");
    self.IfAffixController = self:GetController("IfAffix");
    self.skillName = self:GetChild("BossSkillName");
    self.skillDec = self:GetChild("BossSkillTips");
    self.closeTipBtn = self:GetChild("closeTipsBtn");
    self.Graph_PaoPao = self:GetChild("Graph_PaoPao");
    self.UI_ruchang_bfxw_in = self:GetChild("UI_ruchang_bfxw_in");
    self.shopBtn = self:GetChild("btnShop");--峡湾商店
    self.mainReward = self:GetChild("mainReward")

    self.dailyList = self:GetChild("listReward")
    self.txtTaskTitle = self:GetChild("TaskTitle")


    -- 新英雄挑战活动
    self.occupation1 = self:GetChild("occupation_1")
    self.occupation2 = self:GetChild("occupation_2")
    self.hero_list = self:GetChild("n83")
    -- end

    self.shuiPaoDrawWrapper = nil;
    self.shuiPaoObj = nil;
    self.curTime = 0;
    self.bossBodyUrl = "";
    self.drawWrapper = nil;
    self.bossBody = nil;
    self.spineAnim = nil

    self:addEvent();

    if self.Graph_PaoPao ~= nil then
        local url ="UIEff/UI_efc_yxxqpaopao"

        resMgr:LoadModelPrefab(url, function(go)
            if (self.shuiPaoObj~= nil) then
                destroyImmediate(self.shuiPaoObj);
                self.shuiPaoObj = nil
            end
            if self.shuiPaoDrawWrapper == nil then
                self.shuiPaoDrawWrapper = GoWrapper.New();
            end
            self.shuiPaoObj = newObject(go);
            self.shuiPaoObj:SetActive(true)
            self.shuiPaoObj:AddComponent(TCanvas)
            self.shuiPaoObj.name = url;
            self.shuiPaoDrawWrapper:SetWrapTarget(self.shuiPaoObj, true)
            self.Graph_PaoPao:SetNativeObject(self.shuiPaoDrawWrapper)
        end)
    end

    if change_type == 1 then
        self.ctrl_activity_type:SetSelectedIndex(0)
    else
        -- self.bossNameText.text = "新英雄挑战活动"
        self.ctrl_activity_type:SetSelectedIndex(1)
        self.hero_list.itemRenderer = self.UpdateHeroListItem
        -- local tb_activity_item = ActiveManager.getActiveTableList(change_type)
        local tb_activity_trials_model = G.dataTable["t_activitytrialsmodel"][tostring(change_type)]
        hero_type_data =tb_activity_trials_model.f_HeroTypeUP or {}
        local hero_type1 = hero_type_data[1] or 1
        local hero_type2 = hero_type_data[2] or 1
        self.occupation1:GetChild("icon").url = "ui://ICONAtlas/" .. EnumConst.HeroTypeIcon[hero_type1]
        self.occupation2:GetChild("icon").url = "ui://ICONAtlas/" .. EnumConst.HeroTypeIcon[hero_type2]
        hero_list_data = tb_activity_trials_model.f_HeroUP
        self.hero_list.numItems = #hero_list_data

        activity_title = tb_activity_trials_model.f_TrialsName
        activity_desp = tb_activity_trials_model.f_TrialsBossIntroduce
        activity_boss_battlemap = tb_activity_trials_model.f_BattleMap -- 战斗地图
        activity_trials_bossid = tb_activity_trials_model.f_TrialsBossId -- 战斗boss Id
        activity_trials_times = tb_activity_trials_model.f_TrialsTimes -- 挑战次数
        activity_trials_icon = tb_activity_trials_model.f_TrialsIcon

    end
    --self.CloseFom();
    logicMgr.FuncOpenManager.refreshMainReward(self.mainReward,Config.FuncOpenType.trials)

end

function UIChaosTest.UpdateHeroListItem(index, obj)
    local hero_id = hero_list_data[index+1]
    local qualityBG = obj:GetChild("framebg")
    local frame =obj:GetChild("frame")
    local icon = obj:GetChild("icon"):GetChild("icon")
    -- local name = obj:GetChild("name")
    local typeIcon = obj:GetChild("outplacement")

    local hero_cfg = G.dataTable["t_hero"][tostring(hero_id)]
    if hero_cfg ~= nil then
        local quality = logicMgr.HeroManager.starToQuality(hero_cfg.f_HeroInitialStar)
        frame.url = "ui://ICONAtlas/" .. Config.IconFrame[quality]
        qualityBG.url = "ui://ICONAtlas/" .. Config.IconQuality[quality]
        typeIcon.url = "ui://ICONAtlas/" .. EnumConst.HeroTypeIcon[(hero_type_data[index+1] or 1)]
        icon.url = "ui://ICONAtlas/" .. hero_cfg.f_HeroIcon
    end

end

function UIChaosTest:OnEnable()
    LuaUIMgr.setIgnoreHideUI("BackGround")
    if change_type == 1 then
        resMgr:LoadModelPrefab("UIEff/UI_ruchang_bfxw_in", function(prefab)
            if self.effDrawWrapper == nil then
                self.effDrawWrapper = GoWrapper.New();
            end
            if self.effObj ~= nil then
                destroyImmediate(self.effObj);
                self.effObj = nil
            end
            self.effObj = newObject(prefab)
            self.effDrawWrapper:SetWrapTarget(self.effObj,false)
            Util.Lua_SetLocalRotationXYZ(self.effObj.transform, 0, 0, 0);
            self.UI_ruchang_bfxw_in:SetNativeObject(self.effDrawWrapper)
        end)
        utimer.timeCall(function ()
            UIHeadMenu.isNeedShowMask();
            if not HelperFunc.IsNull(self.effObj) then
                destroyImmediate(self.effObj);
                self.effObj = nil
            end
        end, _bfxwPlayTime, 1);
        -- AudioManager.SetSwitch("battle_steps", "arr")
        -- FightManager.SetBGMSwitchByBattleType(EnumConst.FightTypeConst.TRIAL)
        -- AudioManager.SetSwitch("Battle_SwitchGroup","arena")
        self:refresh();
        self:RegisterEvent(Config.EventType.Trials_Chaos_Info, self.setInfo);
        self:RegisterEvent(Config.EventType.Trials_Chaos_Friend_Rank, self.refreshFriend);
        self:RegisterEvent(Config.EventType.Trials_Chaos_Battle, self.gotoFight);
        self.needShowHeadMenu = nil;
        if isOpenUI(PanelNames.UIHeadMenu) then
            self.needShowHeadMenu = true;
            closeUI(PanelNames.UIHeadMenu);
        end
    else
        -- todo
        self:refresh()
    end
end

function UIChaosTest:refresh()
    if change_type == 1 then
        TrialHandel:SendTrialsInfo()
        TrialHandel.SendTrialsFriendRank()
        TrialHandel.SendTrialsRecordInfo()
    else
        -- todo
        local bossConfig = logicMgr.HeroManager.GetHeroConfigbyModelId(activity_trials_bossid);
        self.bossTypeLoader.url = EnumConst.AtlasPath.ICONAtlas .. EnumConst.HeroTypeIcon[bossConfig.f_Type];
        self.bossNameText.text = getLanguage(bossConfig.f_HeroName).."的试炼";
    end
end

function UIChaosTest:OnDisable()
    LuaUIMgr.setIgnoreHideUI()
    if change_type == 1 then
        self:UnRegisterEvent(Config.EventType.Trials_Chaos_Info, self.setInfo);
        self:UnRegisterEvent(Config.EventType.Trials_Chaos_Friend_Rank, self.refreshFriend);
        self:UnRegisterEvent(Config.EventType.Trials_Chaos_Battle, self.gotoFight);
    else
        -- todo
    end

    if self.idleTimer then
        self.idleTimer:Stop();
    end
    self.idleTimer = nil;
    if self.refreshTimer then
        self.refreshTimer:Stop();
    end
    self.refreshTimer = nil;
end

function UIChaosTest:OnDestroy()
    if self.bossBody then
        destroyImmediate(self.bossBody)
        self.bossBody = nil
    end
    if (self.shuiPaoObj~= nil) then
        destroyImmediate(self.shuiPaoObj);
        self.shuiPaoObj = nil
    end
    self.spineAnim = nil
    change_type = nil
    hero_list_data = nil
    hero_type_data = nil
    activity_title = nil
    activity_desp = nil
    activity_boss_battlemap = nil
    activity_trials_bossid = nil
    activity_trials_times = nil -- 挑战次数
    activity_trials_icon = nil
end

function UIChaosTest:addEvent()
    --btnChangeFormation.onClick:Add(function()
    --    self.ShowSelectFom(1);
    --end);
    self:RegisterOnClick(self.backBtn, self.onCloseClick);
    self:RegisterOnClick(self.tipsBtn, self.onShowTips);
    self:RegisterOnClick(self.rankBtn, self.onOpenRank);
    self:RegisterOnClick(self.finalRewardBtn, self.onOpenRankReward);
    self:RegisterOnClick(self.nextPreviewBtn, self.onOpenNext);
    self:RegisterOnClick(self.battleRecordBtn, self.onOpenRecodeList);
    self:RegisterOnClick(self.challengeBtn, self.onBoss);
    self:RegisterOnClick(self.shopBtn,self.onOpenShop);
    self:RegisterOnClick(self.titleSkill,self.onTitleSkill);
    self:RegisterOnClick(self.closeTipBtn, function()
        self.tipsShowController.selectedIndex = 0;
    end)
end

function UIChaosTest:onShowTips()
    local title_str
    local des_str
    local tips_title_str
    local story
    if change_type == 1 then
        title_str = "ChaoHelpTitle01"
        des_str = "ChaoHelptest1"
        tips_title_str = "ChaoHelpTitle02"
        story = "FunctionStory305"
    else
        title_str = activity_title
        des_str = activity_desp
        tips_title_str = nil
    end

    local txts = {};
    txts["title"] = title_str
    txts["des"] = des_str
    txts["tipsTitle"] = tips_title_str
    txts["story"] = story
    showUI(PanelNames.CommonTips, txts)
end

function UIChaosTest:removeEvent()
    ----bt_SetBtn.onClick:Clear();
    --self.backBtn.onClick:Clear();
    --self.bg.onClick:Clear();
    --self.lastBtn.onClick:Clear();
    --self.nextBtn.onClick:Clear();
    --self.titleDecBackBtn.onClick:Clear();
end

function UIChaosTest:onTitleSkill()
    local msg = logicMgr.TrialsManager.GetTrialsInfo();
    local titleSkillData = G.dataTable["t_skillinfo"][tostring(msg.skillId)];
    if titleSkillData == nil then
        printError("t_skillinfo is nil, skillId ==", msg.skillId)
        return
    end
    self.tipsShowController.selectedIndex = 1;
    self.IfAffixController.selectedIndex = 1;
    self.skillName.text = getLanguage(titleSkillData.f_SkillName);
    self.skillDec.text = getLanguage(titleSkillData.f_Desc);
end

function UIChaosTest:onOpenRank()
    showUI(PanelNames.UIChaosTestRank, nil, change_type)
end

function UIChaosTest:onOpenShop()
    showUI(PanelNames.UIShop, EnumConst.ShopType.trialShop)
end

function UIChaosTest:onCloseClick()
    closeUI(PanelNames.UIChaosTest);
    if change_type == 1 then
        if self.needShowHeadMenu then
            showUI(PanelNames.UIHeadMenu);
            self.needShowHeadMenu = nil;
        end
    else
        -- todo
    end
end

function UIChaosTest:rendererBossSkill(obj)
    --if self.showTitles[1].id == 0 then
    --第一个icon没有
    local self = UIChaosTest;
    skillCount = skillCount + 1;
    local skillId = skills[skillCount];
    local skillData = G.dataTable["t_skillinfo"][tostring(skillId)];
    obj.onClick:Clear();
    obj.onClick:Add(function()
        self.tipsShowController.selectedIndex = 1;
        self.IfAffixController.selectedIndex = 0;
        self.skillName.text = getLanguage(skillData.f_SkillName);
        self.skillDec.text = getLanguage(skillData.f_Desc);
    end);

    if skillData then
        obj:GetChild("icon"):GetChild("icon").url = "ui://SkillAtlas/" .. skillData.f_SkillIcon;
    end
end

function UIChaosTest:RefreshLastDayreward()
    local t = logicMgr.TrialsManager.GetLastDayReward()

    local receive = false
    local updateTime = G.dataTable["t_global"][tostring(35)].f_int_value;
    if t == 0 then

    else
        local date = os.date("%Y-%m-%d", t / 1000 - updateTime)
        local tdate = os.date("%Y-%m-%d", HelperFunc.GetServerTime() - updateTime)
        if date == tdate then
            receive = true
        end
    end

    if receive then
        self.txtTaskTitle.text = getLanguage("ErrorCode_3407")
    else
        self.txtTaskTitle.text = getLanguage("ChaoTips1")
    end
    self.dailyConfig = dataTable.getGlobalString(420);
    local dailyData = {};
    for k, v in pairs(self.dailyConfig) do
        local data = {};
        if k == 1 then
            data[1] = 14;
            local mapConfig = logicMgr.MapManager.getMapcfg();
            if mapConfig then
                data[2] = math.floor(v[1] * mapConfig.f_HolySite * 0.0001 + 0.5) + v[2];
            end
        end
        table.insert(dailyData, data);
    end
    self.dailyList:RemoveChildrenToPool()
    for i = 1, #dailyData do
        local item = self.dailyList:AddItemFromPool()
        if receive then
            dailyData[i][2] = nil;
            item.grayed = true
        else
            item.grayed = false
        end
        logicMgr.CostManager.RefreshItem(item, dailyData[i]);
    end
end

function UIChaosTest:setInfo()
    if change_type == 1 then
        local msg = logicMgr.TrialsManager.GetTrialsInfo();
        if msg.trialsId == 0 then return end
        self.battleInfo = {}
        self.curTime = msg.endTime - HelperFunc.GetServerTime();
        UIChaosTest:refreshTime();
        local cfg = logicMgr.TrialsManager.GetTrialsConfig()[tostring(msg.trialsId)];
        self.battleInfo.trialId = msg.trialsId;
        self.battleInfo.bossId = cfg.f_TrialsBossId;
        self.battleInfo.maxLevel = msg.maxLevel + 1;
        self.battleInfo.skillId = msg.skillId;
        local bossConfig = logicMgr.HeroManager.GetHeroConfigbyModelId(cfg.f_TrialsBossId);
        self.bossTypeLoader.url = EnumConst.AtlasPath.ICONAtlas .. EnumConst.HeroTypeIcon[bossConfig.f_HeroType];
        self.totalDmgText.text = getNumString(msg.maxDamage);
        self.maxRecordText.text = msg.maxLevel;
        local titleSkillData = G.dataTable["t_skillinfo"][tostring(msg.skillId)];
        if titleSkillData == nil then
            printError("t_skillinfo is nil, skillId ==", msg.skillId)
            return
        end
        self.titleSkillName.text = getLanguage(titleSkillData.f_SkillName);
        self.titleSkillDecName.text = getLanguage(titleSkillData.f_SkillName);
        self.titleSkillDecTips.text = getLanguage(titleSkillData.f_Desc);
        local titleSkillShowData = G.dataTable["t_skillshow"][tostring(msg.skillId)];
        if titleSkillShowData ~= nil then
            self.titleSkill:GetChild("n12"):GetChild("icon").url = "ui://SkillAtlas/" .. titleSkillShowData.f_SkillIcon;
        end
        self.bossNameText.text = getLanguage(bossConfig.f_HeroName).."的试炼";
        if self.refreshTimer == nil then
            self.refreshTimer = Timer.New(self.refreshTime, 1, -1, true);
        else
            self.refreshTimer:Reset(self.refreshTime, 1, -1, true);
        end
        self.refreshTimer:Start();
        local showData = {}
        local scale,location = HeroManager.GetHeroLocationResize(cfg.f_TrialsBossId)
        showData.location = location
        showData.scale = scale

        local url = logicMgr.HeroManager.GetHeroDrawingPath(cfg.f_TrialsBossId)
        if self.bossBodyUrl ~= url then
            self.bossBodyUrl = url
            resMgr:LoadModelPrefab(url, function(go)
                local drawWrapper = self.drawWrapper;
                if (drawWrapper == nil) then
                    drawWrapper = GoWrapper.New();
                end
                if (self.bossBody ~= nil) then
                    destroyImmediate(self.bossBody);
                    self.bossBody = nil
                end
                self.bossBody = newObject(go);
                self.bossBody.name = url;
                self.bossBody:AddComponent(TCanvas);
                drawWrapper:SetWrapTarget(self.bossBody, false)
                self.BossModel:SetNativeObject(drawWrapper);
                if self.idleTimer ~= nil then
                    self.idleTimer:Stop();
                end
                Util.Lua_SetLocalPositionXYZ(self.bossBody.transform, showData.location[1], showData.location[2], 0)
                Util.Lua_SetLocalScaleXYZ(self.bossBody.transform, showData.scale[1], showData.scale[2], 1)
                self.spineAnim = nil
                self.spineAnim = self.bossBody:GetComponent("SkeletonGraphic")
                self:PlayShowIdle()
            end);
        else
            self:PlayShowIdle()
        end

        --self.titleCom:GetChild("title").text = getLanguage(cfg.f_TrialsName);
        local skill = nil;
        local data = G.dataTable["t_hero"][tostring(cfg.f_TrialsBossId)]
        local count = 2;
        if #skills <= 0 then
            while true do
                skill = data["f_HeroSkill" .. count];
                if skill == nil or skill == 0 then
                    break ;
                end
                table.insert(skills, skill);
                count = count + 1;
            end
        end
        skillCount = 0;
        self.bossSkillList.itemRenderer = self.rendererBossSkill;
        self.bossSkillList.numItems = #skills;
    else
        -- todo
    end

    self:RefreshLastDayreward()
end

function UIChaosTest:PlayShowIdle()
    if self.spineAnim ~= nil then
        self.spineAnim.AnimationState:SetAnimation(0, "idle2", false);
        local idleAniTime = self.spineAnim.AnimationState.Data.SkeletonData:FindAnimation("idle2").Duration;
        if self.idleTimer == nil then
            self.idleTimer = Timer.New(UIChaosTest.delayIdle, idleAniTime, 1, false);
        else
            self.idleTimer:Reset(UIChaosTest.delayIdle, idleAniTime, 1, false);
        end
        self.idleTimer:Start();
    end
end

function UIChaosTest:refreshTime()
    local self = UIChaosTest;
    self.curTime = self.curTime - 1;
    if self.curTime > 0 then
        self.timeCountText.text = timeToTimeFormat(self.curTime);
    else
        self.timeCountText.text = "00:00:00";
        self:refresh();
        self:RefreshLastDayreward()
    end
end

function UIChaosTest:refreshFriend()
    local friendInfo = logicMgr.TrialsManager.GetFriendRank();
    if friendInfo then
        self.friendCount = 0;
        self.friendInfo = friendInfo;
        self.friendList.itemRenderer = self.rendererRankItem;
        local rendererNum = #friendInfo;
        if rendererNum > 3 then
            rendererNum = 3;
        end
        if rendererNum == 0 then
            self.FriendRank.visible = false;
        else
            self.FriendRank.visible = true;
            self.friendList.height = rendererNum * (self.friendList:AddItemFromPool().height + self.friendList.lineGap);
            self.friendList.numItems = rendererNum;
        end
    end
end

function UIChaosTest:rendererRankItem(obj)
    self = UIChaosTest;
    self.friendCount = self.friendCount + 1;
    local data = self.friendInfo[self.friendCount];
    obj:GetChild("head"):GetChild("head"):GetChild("HeadIcon").url = logicMgr.SkinManager.GetHeroIconBySkinId(data.playerShowInfo.pic)
    obj:GetChild("level").text = tostring(data.level);
    obj:GetChild("PlayerName").text = data.playerShowInfo.name
end

function UIChaosTest:delayIdle()
    -- 调用loop的idea
    if UIChaosTest.spineAnim ~= nil then
        UIChaosTest.spineAnim.AnimationState:SetAnimation(0, "idle1", true);
    end
end

function UIChaosTest.onOpenRankReward()
    showUI(PanelNames.UIChaosTestRewardPreview, nil, change_type);
end

function UIChaosTest.onBoss()
    local cur_change_type = change_type or 1
    local bossFormations = {}
    local trialsId;
    if cur_change_type == 1 then
        local msg = logicMgr.TrialsManager.GetTrialsInfo();
        trialsId = msg.trialsId;
        local cfg = logicMgr.TrialsManager.GetTrialsConfig()[tostring(msg.trialsId)];
        local bossConfig = decodeJsonStrToTabel(dataTable.getDataTableByStringId("t_trialsboss",msg.trialsId,"f_MonsterInfo4"))
        --点击暴风峡湾【挑战首领】
        -- local _trialsBossDic = {}
        -- _trialsBossDic.trials_id = tostring(msg.trialsId)
        -- TAManager.TrackByDictionary("trials_boss_click_challenge",_trialsBossDic)
        for i = 1, 5 do
            local cfgStr = dataTable.getDataTableByStringId("t_trialsboss",msg.trialsId,"f_MonsterInfo" .. i);
            local bossConfig = decodeJsonStrToTabel(cfgStr);
            local bossInfo = {};
            if bossConfig then
                local bossConfig = logicMgr.HeroManager.GetHeroConfigbyModelId(cfg.f_TrialsBossId);
                bossInfo["partnerId"] = tostring(cfg.f_TrialsBossId);
                bossInfo["partnerModelId"] = tostring(cfg.f_TrialsBossId);
                bossInfo["level"] = 999;
                bossInfo["star"] = bossConfig["f_HeroInitialStar"];
            else
                bossInfo["partnerId"] = "0";
                bossInfo["partnerModelId"] = "0";
                bossInfo["level"] = nil;
                bossInfo["star"] = nil;
            end
            table.insert(bossFormations, bossInfo);
        end
    else
        -- todo
        local cfg = logicMgr.TrialsManager.GetTrialsConfig()[tostring(activity_trials_bossid)]
        trialsId = activity_trials_bossid;
        local tb_trialsboss = dataTable.getDataTableByStringId("t_trialsboss",activity_trials_bossid)
        -- local bossConfig = decodeJsonStrToTabel(tb_trialsboss.f_MonsterInfo4)
        if tb_trialsboss ~= nil then
            for i = 1, 5 do
                local bossConfig = decodeJsonStrToTabel(tb_trialsboss["f_MonsterInfo" .. i])
                local bossInfo = {}
                if bossConfig then
                    local boss_cfg = logicMgr.HeroManager.GetHeroConfigbyModelId(cfg.f_TrialsBossId);
                    bossInfo["partnerId"] = tostring(cfg.f_TrialsBossId)
                    bossInfo["partnerModelId"] = tostring(cfg.f_TrialsBossId)
                    bossInfo["level"] = 999
                    bossInfo["star"] = boss_cfg["f_HeroInitialStar"]
                else
                    bossInfo["partnerId"] = "0"
                    bossInfo["partnerModelId"] = "0"
                    bossInfo["level"] = nil
                    bossInfo["star"] = nil
                end
                table.insert(bossFormations, bossInfo)
            end
        end
    end

    showUI(PanelNames.UIBattleTeam, EnumConst.FightTypeConst.TRIAL, bossFormations, function(formations, _, battleInfo)

        if cur_change_type == 1 then
            local info = {}
            info.trialsId = trialsId
            info.formation = formations
            TrialHandel.SendTrialsBattle(info)
        else
            -- todo
        end
    end, UIChaosTest.backFunction)
end

-- 进入战斗
function UIChaosTest:gotoFight(msg)

    if msg.state ~= 0 then return end
    BattlePlaybackManager.setBattleRecord(msg.record)
    BattlePlaybackManager.setBattleResult(msg.fightResult)
    BattlePlaybackManager.requestFightCmdThenPlay(msg.record, EnumConst.FightTypeConst.TRIAL, function()
        UIChaosTest.closeAllUI()
    end, function()
        showUI(PanelNames.UIChaosTest);
        --if logicMgr.ActiveManager.isHaveSevenDay() then
        --    logicMgr.HeroManager.showLocalityUIByName("ChaosTestShowSevenDayUI_", PanelNames.UISevenday);
        --end
    end, nil, self.battleInfo);
end

-- 关闭UI
function UIChaosTest.closeAllUI()
    closeUI(PanelNames.UIBottom);
    --closeUI(PanelResNames.UIHeadMenu);
    --closeUI(PanelNames.UIBattle);
    closeUI(PanelNames.UIFuncMenu);
    closeUI(PanelNames.UIBattleTeam);
    BattleScene.stop();
end

--
function UIChaosTest.onOpenNext()
    showUI(PanelNames.UINextBossPreview, nil, change_type)
end

-- 
function UIChaosTest.onOpenRecodeList()
    showUI(PanelNames.UIChaosTestRecord, nil, change_type)
end

function UIChaosTest.backFunction()

end

return UIChaosTest